/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-11-5
 * @Time  : 下午3:30
 * @brief :
 */
(function () {
    var $page = $(".page-assetsiphone6"),
        $construct = $page.find('.construct'),
        Dialog = require("common:widget/ui/dialog/dialog.js").Dialog;

    P.createPage({
        _data:{
            pid:0,
            summary: P.data('summary')
        },
        _create:function(){
            var tip = new Dialog({
                'width':450,
                'content':'' +
                    '<div style="line-height: 30px;">' +
                    '<div>系统会自动提现到您指定的银行卡</div>' +
                    '<div>如果未绑定银行卡，请在”已绑定的银行卡“后点击”添加“</div>' +
                    '</div>'
            });
            this.data({'tip':tip});
            this.createCycle();
        },

        //事件
        _init:function(){
            var tip = this.data('tip'),
                me = this;
            $construct.find('.btn-recharge-tip').click(function(){
                tip.show();
            });

            $construct.find('.btn-modifybank,.btn-addbank').click(function(e){
                e.stopPropagation();
                $construct.find('.bank-list').html('正在获取...').show();
                me.showBankList();
            });

            $("body").click(function(){
                $construct.find('.bank-list').hide();
            });
        },

        //绑定银行卡
        showBankList:function(){
            var $bankList = $construct.find('.bank-list') ,
                $bankcardContainer = $construct.find('.bankcard-container'),
                noBankTpl = '您还没有添加银行卡，<a href="/user/withdraw">去添加</a>';

            $.get('/bank/getbankcard',function(ret,status){
                if(ret.error_no == 0){
                    var html = '<ul>',
                        list = ret.data;
                    if(list.length){
                        $.each(list,function(k,v){
                            html+='' +
                                '<li ' +
                                'data-id="'+v['id']+'" ' +
                                'data-bank-number="'+ v["bankcard_number"]+'">'+
                                v['bankcard_number'] +
                                '</li>'
                        });
                        html+='</ul>';
                        $bankList.html(html).show().find('li').click(function(e){
                            var bankId = $(this).attr('data-id'),
                                bankcardNumber = $(this).attr('data-bank-number');
                            $.post("/iActivity/bindbank",{
                                bank_id:bankId
                            },function(ret,status){
                                if(ret.error_no == 0){
                                    $bankcardContainer.find('.ob-title').text(mathUtil.edSubstr(bankcardNumber));
                                    $bankcardContainer.find('.ob-info a').text('修改');
                                    $bankList.hide();
                                }
                            },"json");
                        });
                    }else{
                        $bankList.html(noBankTpl);
                    }
                }else{
                    $bankList.html('获取银行卡信息失败')
                }
            },"json")
        },

        //生成饼图
        createCycle:function(){
            var summary = P.data('summary'),
                data = {
                    credit:parseFloat(summary.credit),
                    total_profit:parseFloat(summary.total_profit)
                };

            $page.find('.assets-cycle').highcharts({
                chart : {
                    backgroundColor:'rgba(0,0,0,0)',
                    borderColor:"#AAA"
                },
                credits : {
                    enabled:false
                },
                title: {
                    userHTML : true,
                    text: '' +
                        '<div style="color:#669;font-style:italic;font-size:30px">'+
                            '￥' + mathUtil.formatAmount(data.credit+data.total_profit,4)+
                        '</div>',
                    color:'#FFF',
                    align: 'center',
                    verticalAlign: 'middle',
                    y: -10
                },
                subtitle : {
                    userHTML : true,
                    text: '<div style="font-size:14px;padding-top:30px;color:#336">资金总额</div>',
                    verticalAlign:'middle',
                    y:20
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                legend: {
                    borderColor: '#909090'
                },
                plotOptions: {
                    pie: {
                        borderWidth:"0px",
                        size:"75%",
                        dataLabels: {
                            enabled: true
                        }
                    }
                },

                series: [{
                    type: 'pie',
                    name: '占比',
                    innerSize: '100%',
                    data :[
                        {
                            name:'产品总额',
                            y:parseFloat(data.credit),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#f93'
                        },
                        {
                            name:'收益资金',
                            y:parseFloat(data.total_profit),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#fec284'
                        }
                    ]
                }]
            });
        }
    });
})();